package com.ljy.my_study.java.juc.lock;

/**
 * @author lijunying
 * @date 2021/1/27 17:00
 */
public class TestMain {


    public static void main(String[] args) {
        Runnable runnable=()->{
            test3();
            System.out.println("thread:"+Thread.currentThread().getName()+",state:"+Thread.currentThread().getState()
                    +" in runnable method.");
        };
        Thread thread1=new Thread(runnable);
        Thread thread2=new Thread(runnable);
        thread1.setName("thread1");
        try {
            Thread.sleep(100L);

        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        thread2.setName("thread2");
        thread1.start();
        thread2.start();
        try {
            thread1.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("thread:"+thread1.getName()+",state:"+thread1.getState()+" int the end.");
        System.out.println("thread:"+thread2.getName()+",state:"+thread2.getState()+" int the end.");
    }

    /**
     * 锁的可重入性
     */
    public static synchronized void test1(){
        System.out.println("test1");
        test2();
    }

    public static synchronized void test2(){
        System.out.println("test2");
    }

    public static synchronized void test3(){
        System.out.println("test3 start");
        try {
            Thread.sleep(1000L);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("test3 end");
    }
}
